"""
Case Type   : 基础功能
Case Name   : 删除复制槽后删除订阅端
Create At   : 2022/3/4
Owner       : opentestcase026
Description :
    1.创建发布订阅
    2.停止订阅端
    3.删除复制槽
    4.重启订阅端
    5.删除订阅端
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
History     :
    Modified by opentestcase012 2022/10/28:研发代码变更,修改删除订阅的断言,修改用例格式
    modified by opentestcase013 at 2023-9-18:研发代码变更，修改删除订阅断言为删除成功
"""
import unittest
import os
from yat.test import macro
from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class Pubsubclass(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info("-----------this is setup-----------")
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_userdb_pub = Node(node='PrimaryDbUser')
        self.pri_userdb_sub = Node(node='remote1_PrimaryDbUser')
        self.constant = Constant()
        self.commsh_pub = CommonSH('PrimaryDbUser')
        self.commsh_sub = CommonSH('remote1_PrimaryDbUser')
        self.com_pub = Common()
        self.subname = "sub_case121"
        self.pubname = "pub_case121"
        self.hba_pub = os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')
        self.hba_sub = os.path.join(macro.DB_INSTANCE_PATH_REMOTE1,
                                    'pg_hba.conf')
        self.hba_bak_pub = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH), 'pg_hba.conf')
        self.hba_bak_sub = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH_REMOTE1), 'pg_hba.conf')
        self.port = str(int(self.pri_userdb_pub.db_port) + 1)
        self.wal_level = self.com_pub.show_param("wal_level")
        self.user_param_pub = f'-U {self.pri_userdb_pub.db_user} ' \
            f'-W {self.pri_userdb_pub.db_password}'
        self.user_param_sub = f'-U {self.pri_userdb_sub.db_user} ' \
            f'-W {self.pri_userdb_sub.db_password}'

        text = '-----预置条件1:备份配置文件pg_hba.conf-----'
        self.log.info(text)
        cmd = f'cp {self.hba_pub} {self.hba_bak_pub};'
        self.log.info(cmd)
        msg1 = self.com_pub.get_sh_result(self.pri_userdb_pub, cmd)
        cmd = f'cp {self.hba_sub} {self.hba_bak_sub};'
        self.log.info(cmd)
        msg2 = self.com_pub.get_sh_result(self.pri_userdb_sub, cmd)
        self.assertEqual('', msg1 and msg2, '执行失败:' + text)

        text = '-----预置条件2:查看发布端和订阅端的数据库状态-----'
        self.log.info(text)
        status = self.commsh_pub.get_db_cluster_status()
        self.log.info(status)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status, '执行失败:' + text)
        status = self.commsh_sub.get_db_cluster_status(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(status)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status, '执行失败:' + text)

        text = '-----预置条件3:修改发布端pg_hba文件和配置参数wal_level=logical，重启数据库-----'
        self.log.info(text)
        guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host    replication  {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_sub.db_host}/32 sha256')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败:' + text)
        if 'logical' != self.wal_level:
            result = self.commsh_pub.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG, 'wal_level=logical')
            self.log.info(result)
            self.assertTrue(result, '执行失败:' + text)
            result = self.commsh_pub.restart_db_cluster(False)
            self.assertTrue(result, '执行失败:' + text)
            status = self.commsh_pub.get_db_cluster_status()
            self.log.info(status)
            self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status,
                          '执行失败:' + text)

        text = '-----预置条件4:订阅端修改pg_hba文件-----'
        self.log.info(text)
        guc_res = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host    replication  {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_pub.db_host}/32 sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败:' + text)

    def test_pubsub(self):
        text = '--step1:创建发布订阅 expect:成功--'
        self.log.info(text)
        sql = f"drop publication if exists {self.pubname};" \
            f"create publication {self.pubname} for all tables;"
        result = self.commsh_pub.execut_db_sql(sql,
                                               self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.create_pub_succ_msg, result,
                      '执行失败:' + text)
        self.assertNotIn(self.constant.SQL_WRONG_MSG[1], result,
                         '执行失败:' + text)
        result = self.commsh_sub.execute_generate(
            macro.COMMON_PASSWD, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertIn('', result, '执行失败:' + text)
        sql = f"drop subscription if exists {self.subname};" \
            f"create subscription {self.subname} connection " \
            f"'host={self.pri_userdb_pub.db_host} " \
            f"port={self.port} " \
            f"user={self.pri_userdb_pub.db_user} " \
            f"dbname={self.pri_userdb_pub.db_name} " \
            f"password={self.pri_userdb_pub.ssh_password}' " \
            f"publication {self.pubname};"
        result = self.commsh_sub.execut_db_sql(sql, self.user_param_sub, None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn(self.constant.create_sub_succ_msg,
                      result, '执行失败:' + text)

        text = '--step2:停止订阅端 expect:成功--'
        self.log.info(text)
        result = self.commsh_sub.stop_db_cluster(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(result, '执行失败:' + text)

        text = '--step3:删除复制槽 expect:成功--'
        self.log.info(text)
        sql = f"select * from pg_drop_replication_slot('{self.subname}');"
        result = self.commsh_pub.execut_db_sql(sql,
                                               self.user_param_pub)
        self.log.info(result)
        self.assertIn('1 row', result, '执行失败:' + text)
        self.assertNotIn(self.constant.SQL_WRONG_MSG[1], result,
                         '执行失败:' + text)

        text = '--step4:重启订阅端 expect:成功--'
        self.log.info(text)
        result = self.commsh_sub.start_db_cluster(
            env_path=macro.DB_ENV_PATH_REMOTE1)

        text = '--step5:删除订阅端 expect:成功--'
        self.log.info(text)
        self.assertTrue(result, '执行失败:' + text)
        sql = f"drop subscription  {self.subname};" \
            f"select * from pg_subscription;"
        result = self.commsh_sub.execut_db_sql(sql, self.user_param_sub, None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn(self.constant.drop_sub_succ_msg,
                      result, '执行失败:' + text)
        self.assertIn('0 rows', result, '执行失败:' + text)
        self.assertIn(self.constant.drop_sub_succ_msg, result,
                         '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text1 = '-----订阅端数据库启动集群查看集群状态 expect:成功-----'
        self.log.info(text1)
        status_sub = self.commsh_sub.get_db_cluster_status(
            'status', env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(status_sub)
        if not status_sub:
            self.commsh_sub.start_db_cluster(
                env_path=macro.DB_ENV_PATH_REMOTE1)
            status_sub = self.commsh_sub.get_db_cluster_status(
                'status', env_path=macro.DB_ENV_PATH_REMOTE1)
            self.log.info(status_sub)

        text2 = '-----删除发布订阅 expect:成功-----'
        self.log.info(text2)
        sql = f"drop subscription if exists {self.subname};"
        drop_sub_result = self.commsh_sub.execut_db_sql(
            sql, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(drop_sub_result)
        sql = f"drop publication if exists {self.pubname};"
        drop_pub_result = self.commsh_pub.execut_db_sql(
            sql, sql_type=self.user_param_pub)
        self.log.info(drop_pub_result)

        text3 = '-----订阅端和发布端恢复pg_hba.conf文件 expect:成功-----'
        self.log.info(text3)
        cmd = f'if [[ -e {self.hba_pub} && -e {self.hba_bak_pub} ]]; ' \
            f'then rm -rf {self.hba_pub}; ' \
            f'mv {self.hba_bak_pub} {self.hba_pub}; ' \
            f'else echo not exits; fi'
        hba_msg_pub = self.com_pub.get_sh_result(self.pri_userdb_pub, cmd)
        cmd = f'if [[ -e {self.hba_sub} && -e {self.hba_bak_sub} ]]; ' \
            f'then rm -rf {self.hba_sub}; ' \
            f'mv {self.hba_bak_sub} {self.hba_sub}; ' \
            f'else echo not exits; fi'
        hba_msg_sub = self.com_pub.get_sh_result(self.pri_userdb_sub, cmd)

        text4 = '-----发布端恢复参数配置,重启数据库查看数据库状态 expect:成功-----'
        self.log.info(text4)
        guc_wal_pub = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'wal_level={self.wal_level}')
        res_msg_pub = self.commsh_pub.restart_db_cluster(False)
        status_pub = self.commsh_pub.get_db_cluster_status()
        self.log.info(status_pub)

        self.assertTrue(status_sub, '执行失败:' + text1)
        self.assertIn(self.constant.drop_sub_succ_msg, drop_sub_result,
                      '执行失败' + text2)
        self.assertIn(self.constant.drop_pub_succ_msg, drop_pub_result,
                      '执行失败' + text2)
        self.assertEqual('', hba_msg_pub and hba_msg_sub, '执行失败:' + text3)
        self.assertTrue(guc_wal_pub, '执行失败:' + text4)
        self.assertTrue(res_msg_pub, '执行失败:' + text4)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status_pub,
                      '执行失败:' + text4)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
